use crate::formats::tiff::IFD;

use super::NikonMakernote;
use crate::Result;

pub(super) fn nef_decrypt(buf: &mut [u8], start: usize, makernote: &IFD) -> Result<()> {
  let serial = fetch_tiff_tag!(makernote, NikonMakernote::NefSerial);
  let data = serial.get_data();
  let mut serialno = 0_usize;
  for i in 0..serial.count() as usize {
    if data[i] == 0 {
      break;
    }
    serialno = serialno * 10
      + if data[i] >= 48 && data[i] <= 57 {
        // "0" to "9"
        (data[i] - 48) as usize
      } else {
        (data[i] % 10) as usize
      };
  }

  // Get the "decryption" key
  let keydata = fetch_tiff_tag!(makernote, NikonMakernote::NefKey).force_u32(0).to_le_bytes();
  let keyno = (keydata[0] ^ keydata[1] ^ keydata[2] ^ keydata[3]) as usize;

  let src = &mut buf[start..];

  let ci = WB_SERIALMAP[serialno & 0xff] as u32;
  let mut cj = WB_KEYMAP[keyno & 0xff] as u32;
  let mut ck = 0x60_u32;
  for i in 0..src.len() {
    cj += ci * ck;
    ck += 1;
    src[i] ^= cj as u8;
  }
  Ok(())
}

// We use this for the D50 and D2X whacky WB "encryption"
const WB_SERIALMAP: [u8; 256] = [
  0xc1, 0xbf, 0x6d, 0x0d, 0x59, 0xc5, 0x13, 0x9d, 0x83, 0x61, 0x6b, 0x4f, 0xc7, 0x7f, 0x3d, 0x3d, 0x53, 0x59, 0xe3, 0xc7, 0xe9, 0x2f, 0x95, 0xa7, 0x95, 0x1f,
  0xdf, 0x7f, 0x2b, 0x29, 0xc7, 0x0d, 0xdf, 0x07, 0xef, 0x71, 0x89, 0x3d, 0x13, 0x3d, 0x3b, 0x13, 0xfb, 0x0d, 0x89, 0xc1, 0x65, 0x1f, 0xb3, 0x0d, 0x6b, 0x29,
  0xe3, 0xfb, 0xef, 0xa3, 0x6b, 0x47, 0x7f, 0x95, 0x35, 0xa7, 0x47, 0x4f, 0xc7, 0xf1, 0x59, 0x95, 0x35, 0x11, 0x29, 0x61, 0xf1, 0x3d, 0xb3, 0x2b, 0x0d, 0x43,
  0x89, 0xc1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